package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.Version;
import com.central.app.service.HandlerService;
import com.central.apps.dao.BlMessageDao;
import com.central.apps.entity.BlMessage;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.apps.service.MessageService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消息应用处理程序
 *
 * @author JHY
 * @date 2022/11/07
 */
@Slf4j
@Component
public class MessageAppHandler extends LvBaseHandler {
    @Resource
    private MessageService messageService;
    @Resource
    private BlMessageDao messageDao;
    @Autowired
    private HandlerService handlerService;

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        System.out.println(" =====> enter afterSave MessageAppHandler <===== ");
        System.out.println(app);
        System.out.println(objName);
        System.out.println(JSON.toJSONString(data));
        System.out.println(isUpdate);
        String recipient = MapUtil.getStr(data, "recipient");
        String messagenum = MapUtil.getStr(data, "messagenum");
        String isshow = MapUtil.getStr(data, "isshow");
        String title = MapUtil.getStr(data, "title");
        String createuserdesc = MapUtil.getStr(data, "createuserdesc");
        String content = MapUtil.getStr(data, "content");
        String createuser = MapUtil.getStr(data, "createuser");
        String version = MapUtil.getStr(data, "version");
        String recipientname = MapUtil.getStr(data, "recipientname");
        Date createtime = MapUtil.getDate(data, "createtime");
        String recipientselect = MapUtil.getStr(data, "recipientselect");
        if ("接收人".equals(recipientselect)) {
            if (recipient.indexOf(",") > 0) {
                String id = MapUtil.getStr(data, "id");
                messageDao.deleteById(id);
                String[] recipientArr = recipient.split(",");
                String[] recipientnameArr = recipientname.split(",");
                for (int i = 0; i < recipientArr.length; i++) {
                    BlMessage message = new BlMessage();
                    message.setCreatetime(createtime);
                    message.setIsshow(isshow);
                    message.setContent(content);
                    message.setTitle(title);
                    message.setMessagenum(messagenum);
                    message.setCreateuser(createuser);
                    message.setCreateuserdesc(createuserdesc);
                    message.setStatus("未读");
                    message.setRecipient(recipientArr[i]);
                    message.setRecipientname(recipientnameArr[i]);
                    message.setVersion(version);
                    int res = messageService.addMessage(message);
                    System.out.println("添加状态:" + res);
                }
            }
        } else {
            // 用户角色
            String roleid = MapUtil.getStr(data, "roleid");
            String rolecode = MapUtil.getStr(data, "rolecode");
            String rolename = MapUtil.getStr(data, "rolename");
            String roledeptname = MapUtil.getStr(data, "roledeptname");
            List<Map<String, Object>> rolesList =
                    handlerService
                            .findBySql("SELECT u.* FROM sys_user u LEFT JOIN sys_role_user sru ON u.worknumber = sru.userworknumber WHERE sru.rolecode = '" + rolecode + "'");
            if (rolesList.size() > 0) {
                for (Map<String, Object> item : rolesList) {
                    String id = MapUtil.getStr(item, "id");
                    String nickname = MapUtil.getStr(item, "nickname");
                    BlMessage message = new BlMessage();
                    message.setCreatetime(createtime);
                    message.setIsshow(isshow);
                    message.setContent(content);
                    message.setTitle(title);
                    message.setMessagenum(messagenum);
                    message.setCreateuser(createuser);
                    message.setCreateuserdesc(createuserdesc);
                    message.setStatus("未读");
                    message.setRoleid(roleid);
                    message.setRolecode(rolecode);
                    message.setRolename(rolename);
                    message.setRoledeptname(roledeptname);
                    message.setVersion(version);
                    message.setRecipient(id);
                    message.setRecipientname(nickname);
                    int res = messageService.addMessage(message);
                    System.out.println("角色添加状态:" + res + ",接收人ID:" + id + ",接收人名称:" + nickname);
                }
            }

        }


        System.out.println(" =====> end afterSave MessageAppHandler <===== ");
        super.afterSave(app, objName, data, isUpdate);
    }
}
